package com.cloud.tsp.controller;

import com.cloud.core.util.R;
import com.cloud.api.domain.forward.ResponseData;
import com.cloud.api.domain.tsp.QueryVehicleProduction;
import com.cloud.api.domain.tsp.VehicleProduction;
import com.cloud.api.constants.StrVo;
import com.cloud.log.annotation.Log;
import com.cloud.log.enums.BusinessType;
import com.cloud.log.enums.OperatorType;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.service.IVehicleProductionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author haohaibo
 * @since 2023-12-20
 */
@RestController
@Validated
@RequestMapping("/vehicle-production")
public class VehicleProductionController {


    @Resource
    private IVehicleProductionService vehicleProductionService;

    @Tag(name = "v1.0")
    @Operation(summary = "生产数据（管理）")
    @PostMapping("/getVehicleProductionList")
    @Log(title = "生产车辆管理-生产数据", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public TableDataInfo<VehicleProduction> selectProductionList(@RequestBody @Valid QueryVehicleProduction para){
        return vehicleProductionService.selectProductionList(para);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "新增或修改生产数据（管理）")
    @PostMapping("/insertOrUpdate")
    @Log(title = "生产车辆管理-新增或修改生产数据", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R insertOrUpdate(@RequestBody @Valid VehicleProduction para){
        vehicleProductionService.insertOrUpdate(para);
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "生产数据详情（管理）")
    @PostMapping("/getById")
    @Log(title = "生产车辆管理-生产数据详情", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<VehicleProduction> getById(@RequestBody @Valid StrVo para){
        return R.ok(vehicleProductionService.selectById(para.getId()));
    }

    /*@Tag(name = "v1.0")
    @Operation(summary = "通过vin找车型（管理）")
    @PostMapping("/getByVin")
    @Log(title = "生产车辆管理-通过vin找车型", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<VehicleProduction> getByVin(@RequestBody @Valid StrVo para){
        return R.ok(vehicleProductionService.selectByVin(para.getId()));
    }*/

    @Tag(name = "v1.0")
    @Operation(summary = "通过vin找车型名称和设备编号（管理）")
    @PostMapping("/getNameByVin")
    @Log(title = "生产车辆管理-通过vin找车型名称和设备编号", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<String> getNameByVin(@RequestBody @Valid StrVo para){
        return R.ok("操作成功",vehicleProductionService.getNameByVin(para.getId()));
    }

    @Tag(name = "v1.0")
    @Operation(summary = "生产数据删除（管理）")
    @PostMapping("/deleteById")
    @Log(title = "生产车辆管理-生产数据删除", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R deleteById(@RequestBody @Valid StrVo para){
        vehicleProductionService.deleteById(para.getId());
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "查询vin是否存在")
    @PostMapping("/checkExist")
    @Log(title = "生产车辆管理-查询vin是否存在", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R checkExist(@RequestBody @Valid StrVo para) throws IOException {
        return R.ok(vehicleProductionService.checkExist(para.getId()));
    }

    /*@Tag(name = "v1.0")
    @Operation(summary = "联想vin，用于销售数据模块")
    @PostMapping("/getProductionVinList")
    @Log(title = "生产车辆管理-联想vin，用于销售数据模块", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<StrVo> selectProductionVinList(@RequestBody @Valid StrVo para) {
        return R.ok(vehicleProductionService.selectProductionVinList(para.getId()));
    }*/


    @Tag(name = "v1.0")
    @Operation(summary = "下载模板")
    @PostMapping("/downloadTemplate")
    @Log(title = "生产车辆管理-下载模板", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE)
    public void downloadTemplate(@NotEmpty String vehicleType ,HttpServletResponse response) throws IOException {
        vehicleProductionService.downloadTemplate(vehicleType,response);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "导出生产数据")
    @PostMapping("/exportProductionVinList")
    @Log(title = "生产车辆管理-导出生产数据", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE)
    public void exportProductionVinList(QueryVehicleProduction para, HttpServletResponse response) throws IOException {
        vehicleProductionService.exportProductionVinList(para,response);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "导入生产数据表")
    @PostMapping("/importProductionVinList")
    @Log(title = "生产车辆管理-导入生产数据表", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    public R<ResponseData> importProductionVinList(@RequestParam MultipartFile file, @NotEmpty String dept,@NotEmpty String vehicleType, HttpServletResponse response) throws IOException {
        String key = "product";
        ResponseData responseData = vehicleProductionService.importProductionVinList(dept,vehicleType, file, response, key);
        return R.ok(responseData);
    }
}